/**
 * Created by Anard on 2017/4/19.
 */
let WebSocketServer = require('ws').Server;
let net = require('net');
//let dnode = require('dnode');
let rpc = require('./rpc.js');
let push = require('./push.js');
let cron = require('./cron.js');
let log = require('../util/log.js');
let gameData = require('../util/gameData.js');
let mem = require('../util/mem.js');
let util = require('../util/util.js');
let action = require('./action.js');

let argv = process.argv.splice(2);
gameData.init();
util.setServerInfo('gate', argv[0]);

rpc.dc();
rpc.enter();
rpc.match();
rpc.game();
rpc.func();
rpc.activity();
//rpc.arena();

let wss = new WebSocketServer({port: util.getServerInfo('port')});
log.p("门服务器启动:" + util.getServerInfo('port'));
wss.on('connection', function (socket) {
    //log.p('connection');
    let id = util.uuid();
    socket.uuid = id;
    mem.clients[id] = socket;
    mem.clients[id].chip = null;
    socket.updateTime = Date.now();
    socket.on('message', function (message) {
        action.message(message, socket, id);
    });

    // 结束处理
    socket.on('close', function () {
        //log.d('close');
        action.close(socket);
    });
    // 错误处理
    socket.on('error', function (e) {
        log.e('error:' + e);
        action.close(socket);
    });
});
cron.init();